package com.jsgt.greekfire.ui.wallet.assets

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jsgt.greekfire.ENTRUST_NUM
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.TradeOrder
import com.jsgt.greekfire.common.util.TimeUtil
import com.jsgt.greekfire.common.util.UtilsBigDecimal

class TradeOrderAdapter :
    BaseQuickAdapter<TradeOrder, BaseViewHolder>(R.layout.item_trade_order) {
    var address: String? = null

    override fun convert(holder: BaseViewHolder, item: TradeOrder) {
        var side = 0
        var typeStr = ""
        when (item.contractType) {
            TradeOrder.Contract.CONTRACT_TRANSFER -> {
                if (address == item.fromAddress) {
                    side = 1
                }
                typeStr = String.format(context.getString(R.string.transfer_title1), item.assetName)
            }
            TradeOrder.Contract.CONTRACT_DELEGATE -> {
                side = 1
                typeStr = context.getString(R.string.entrust_mining)
            }
            TradeOrder.Contract.CONTRACT_UNDELEGATE -> {
                typeStr = context.getString(R.string.entrust_mining_quit)
            }
            TradeOrder.Contract.CONTRACT_DELEGATE_REWARD -> {
                typeStr = context.getString(R.string.entrust_mining_reward)
            }
        }

        holder.setText(
            R.id.txvId, if (typeStr.isEmpty()) {
                item.transactionId
            } else {
                typeStr
            }
        )
            .setText(
                R.id.txvAmount,
                if (side == 0) {
                    "+"
                } else {
                    "-"
                } + UtilsBigDecimal.div(item.assetAmount.toString(), ENTRUST_NUM)
                    .toString() + item.assetName
            )
            .setText(
                R.id.txvStatus, context.getString(
                    if ("SUCCESS" == item.result) {
                        R.string.transfer_status_success
                    } else {
                        R.string.transfer_status_failed
                    }
                )
            )
            .setText(R.id.txvTime, TimeUtil.time2String(item.timeStamp, "MM/dd HH:mm"))
            .setImageResource(
                R.id.imvSide, if (side == 0) {
                    R.mipmap.ic_trade_in
                } else {
                    R.mipmap.ic_trade_out
                }
            )
    }
}